package com.veo.service;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.veo.entity.*;
import com.veo.mapper.OrderMapper;
import com.veo.utils.OrderUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class OrderService extends ServiceImpl<OrderMapper, Order> {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private OrderItemService orderItemService;

    @Autowired
    private CartService cartService;

    /**
     * 购买
     * @param cartVos
     * @param session
     * @return
     */
    public String buy(List<CartVo> cartVos,Integer addressId, HttpSession session) {
        //1.生成订单表记录
        Order order = new Order();
        User user = (User) session.getAttribute("user");
        order.setAddressId(addressId);
        order.setUserId(user.getId());
        order.setCreateDate(new Date());
//        order.setOrderNum(UUID.randomUUID().toString());
        //生成自定义编号
        order.setOrderNum(OrderUtils.createOrderNum());
        order.setOrderStatus("1");
        orderMapper.insert(order);


        //2.生成订单详细记录
        List<OrderItem> orderItems = new ArrayList<>();
        List<Integer> cartIds = new ArrayList<>();
        for (CartVo cartVo : cartVos) {
            OrderItem orderItem = new OrderItem();
            orderItem.setBookId(cartVo.getBookId());
            orderItem.setOrderId(order.getId());
            orderItem.setCount(cartVo.getCount());
            orderItems.add(orderItem);
            cartIds.add(cartVo.getId());
        }
        orderItemService.saveBatch(orderItems);

        //3.删除购物车中的记录
        cartService.removeByIds(cartIds);
        return "success";
    }

    /**
     * 查询用户订单
     */
    public List<Order> findUserOrder(Integer userId, OrderQueryVo orderQueryVo){
        Integer begin = (orderQueryVo.getPage() - 1) * orderQueryVo.getPageSize();
        Integer end = orderQueryVo.getPage() * orderQueryVo.getPageSize();
        orderQueryVo.setUserId(userId);
        orderQueryVo.setBegin(begin);
        orderQueryVo.setEnd(end);
        List<Order> list = orderMapper.findOrderAnderOrderDetailListByUser(orderQueryVo);
        for (Order order : list) {
            List<OrderItem> orderItems = order.getOrderItems();
            double price = 0.0;
            for (OrderItem orderItem : orderItems) {
                price += orderItem.getCount() * orderItem.getBook().getNewPrice();
            }
            //计算订单的总金额
            order.setTotalPrice(price);
        }
        return list;
    }

    /**
     * 查询总页数
     * @param id
     * @param pageSize
     * @return
     */
    public Integer findUserOrderPages(OrderQueryVo orderQueryVo) {
        Integer total = orderMapper.findOrderCountByUser(orderQueryVo);
        Integer pageSize = orderQueryVo.getPageSize();
        return  total % pageSize == 0 ? total / pageSize : total / pageSize + 1;
    }

    /**
     * 获取所有订单信息
     * @return
     */
    public List<Order> findAllOrder(){
        return orderMapper.findAllOrder();
    }


    /**
     * 根据id查询订单
     * @return
     */
    public Order findOrderById(Integer orderId){
        Order order = orderMapper.findOrderById(orderId);
        double totalPrice = 0.0;
        List<OrderItem> orderItems = order.getOrderItems();
        for (OrderItem orderItem : orderItems) {
            totalPrice += orderItem.getBook().getNewPrice() * orderItem.getCount();
        }
        order.setTotalPrice(totalPrice);
        return order;
    }
}
